package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyPlanDetailsAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.MyPlanDetailData;
import com.kkd.kkdapp.http.entity.MyPlanDetailDataItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

/****
 * 
 * @Date 2016-6-1
 * 
 * @author mty
 * 
 * @Name 投标详情
 * 
 */
public class MyPlanDetailsActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private final static int CODE_ZERO = 1;
	private final static int CODE_ELSE = 2;
	private DecimalFormat df = new DecimalFormat("0.00");
	private RelativeLayout td_top_img;
	private TextView bid_name; // 标的名称
	private String bidTitle;
	private TextView bid_state; // 标的状态
	private String bidStatus;
	private String bidStatusCode;
	private TextView investment_amount; // 投资金额
	private String poiAmount;
	private TextView interest_rate; // 年化收益
	private String bidlilv;
	private TextView term; // 期限
	private String bidPeriodName;
	private TextView date; // 投标时间
	private String poiDate;
	private KkdXlistView mListview;
	private List<MyPlanDetailDataItem> billList = new ArrayList<MyPlanDetailDataItem>();
	private MyPlanDetailsAdapter adapter;
	private TextView agreement_bt; // 借款合同Button
	private String agreeloanUrl;
	private String appbiddetail;
	private LinearLayout top;
	private String bidId;
	private String mPoiId;
	private String orderId;
	private String financeId;
	private ImageView id_no_content;
	private TextView id_no_content_text;
	private ListView listView;
	private String poiId;
	private int status;
	private DecimalFormat df2 = new DecimalFormat("#,###");
	private String xyDesc;// 服务协议
	private String contractsDetailUrl;// 服务协议url
	private String contractUrl; // 服务协议PDFurl

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				agreement_bt.setText(xyDesc);
				bid_name.setText(bidTitle);
				if (status == 40) {
					bid_state.setBackgroundResource(R.drawable.bidding_bg);
					bid_state.setText("募集中");
				} else if (status == 50) {
					bid_state.setBackgroundResource(R.drawable.bidding_bg);
					bid_state.setText("已投满");
				} else if (status == 60) {
					bid_state.setBackgroundResource(R.drawable.repaymentin_bg);
					bid_state.setText("还款中");
				} else if (status == 70) {
					bid_state.setBackgroundResource(R.drawable.haspaidoff_bg);
					bid_state.setText("已还清");
				}
				investment_amount.setText(poiAmount);
				interest_rate.setText(bidlilv);
				term.setText(bidPeriodName);
				date.setText(poiDate);
				if (billList != null) {
					adapter = new MyPlanDetailsAdapter(
							MyPlanDetailsActivity.this, billList);
					mListview.setAdapter(adapter);
				}
				if (billList != null && billList.size() == 0) {
					id_no_content.setVisibility(View.VISIBLE);
					id_no_content_text.setVisibility(View.VISIBLE);
					id_no_content_text.setText("暂无回款计划，将在该计划放款后生成");
				} else if (billList.size() > 0) {
					id_no_content.setVisibility(View.GONE);
					id_no_content_text.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();
				mListview.stopRefresh();
				mListview.initState();
				break;
			case CODE_ELSE:
				if (billList == null) {
					id_no_content.setVisibility(View.VISIBLE);
					id_no_content_text.setVisibility(View.VISIBLE);
					id_no_content_text.setText("暂无回款计划，将在该计划放款后生成");
				} else {
					if (billList.size() == 0) {
						id_no_content.setVisibility(View.VISIBLE);
						id_no_content_text.setVisibility(View.VISIBLE);
						id_no_content_text.setText("暂无回款计划，将在该计划放款后生成");
					}

				}
				break;
			}
		};
	};
	private LinearLayout linear_toZhaiquanpipei;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bid_details);
		initView();
		getMessage();
		setListener();
	}

	public void initView() {
		td_top_img = (RelativeLayout) this.findViewById(R.id.td_top_img);
		mListview = (KkdXlistView) this.findViewById(R.id.manage_lv);
		listView = mListview.mListView;
		listView.setDividerHeight(0);
		mListview.startRefresh();
		agreement_bt = (TextView) this.findViewById(R.id.agreement_bt);
		id_no_content = (ImageView) this.findViewById(R.id.id_no_content);
		id_no_content_text = (TextView) this
				.findViewById(R.id.id_no_content_text);
		View view = LayoutInflater.from(this).inflate(
				R.layout.biddetails_listview_head, null);
		bid_name = (TextView) view.findViewById(R.id.bid_name);
		bid_name.setSelected(true);
		bid_state = (TextView) view.findViewById(R.id.bid_state);
		investment_amount = (TextView) view
				.findViewById(R.id.investment_amount);
		interest_rate = (TextView) view.findViewById(R.id.interest_rate);
		term = (TextView) view.findViewById(R.id.term);
		date = (TextView) view.findViewById(R.id.date);
		top = (LinearLayout) view.findViewById(R.id.top);
		linear_toZhaiquanpipei = (LinearLayout) view
				.findViewById(R.id.linear_toZhaiquanpipei);
		linear_toZhaiquanpipei.setVisibility(View.VISIBLE);
		listView.addHeaderView(view);
	}

	public void setListener() {
		td_top_img.setOnClickListener(this);
		agreement_bt.setOnClickListener(this);
		mListview.setKKDScrollListener(this);
		top.setOnClickListener(this);
		linear_toZhaiquanpipei.setOnClickListener(this);
	}

	public void getMessage() {
		Intent intent = this.getIntent();
		poiId = intent.getStringExtra("poiId");
		requestData(poiId);
	}

	@Override
	public void onClick(View v) {

		Intent intent;

		switch (v.getId()) {
		case R.id.td_top_img:
			finish();
			break;
		case R.id.agreement_bt:
			if (!TextUtils.isEmpty(contractsDetailUrl)) {
				intent = new Intent(this, KkdPdfWebActivity.class);
				intent.putExtra("url", contractsDetailUrl);
				intent.putExtra("pdfUrl", contractUrl);
				intent.putExtra("fileName", "【KKD】" + xyDesc + "_" + orderId
						+ ".pdf");
				startActivity(intent);
			}
			break;
		case R.id.top:
			LogUtil.e("TAG", "-----------------");
			if (!TextUtils.isEmpty(orderId) && !TextUtils.isEmpty(financeId)
			/* && !TextUtils.isEmpty(appbiddetail) */) {
				intent = new Intent(this,
						FinancialPlanParticularsActivity.class);
				intent.putExtra("poiId", orderId);
				intent.putExtra("bidId", financeId);
				intent.putExtra("appbiddetail", appbiddetail);
				startActivity(intent);
			}
			break;
		case R.id.linear_toZhaiquanpipei:
			intent = new Intent(this, MyMatchingRecordActivity.class);
			intent.putExtra("financeId", financeId);
			intent.putExtra("orderId", orderId);
			startActivity(intent);
			break;
		}

	}

	/**
	 * 请求接口
	 */
	public void requestData(String poiId) {
		String customerIdAndToken = PreferenceUtils.getInstance()
				.getcustomerId(this);
		String[] cat = customerIdAndToken.split("&");
		if (cat == null || cat.length < 2)
			return;

		KkdHttpMagager.getInstance().getMyPlanDetailManager(poiId,
				MyPlanDetailData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						MyPlanDetailData data = (MyPlanDetailData) responseBody;
						bidTitle = data.getTitle();
						status = data.getFinanceStatus();
						poiAmount = df2.format(data.getFinanceAmount() / 100);
						String preYearRate = "";
						if (data.getPreYearRate() % 100 == 0) {
							preYearRate = data.getPreYearRate() / 100 + "%";
						} else {
							preYearRate = data.getPreYearRate() * 0.01 + "%";
						}
						if (data.getActivityRate() == 0) {
							bidlilv = preYearRate;
						} else {
							String activityRate = "";
							if (data.getActivityRate() % 100 == 0) {
								activityRate = data.getActivityRate() / 100
										+ "%";
							} else {
								activityRate = data.getActivityRate() * 0.01
										+ "%";
							}
							bidlilv = preYearRate + "+" + activityRate;
						}
						bidPeriodName = data.getFinancePeriodWeb();
						poiDate = data.getFinanceDateString();
						billList = data.getBillList();
						agreeloanUrl = data.getAgreeloanUrl();
						appbiddetail = data.getFinanceDesc();
						orderId = data.getOrderId();
						financeId = data.getFinanceId();
						xyDesc = data.getXyDesc();
						contractsDetailUrl = data.getContractsDetailUrl();
						contractUrl = data.getContractUrl();
						mHandler.sendEmptyMessage(CODE_ZERO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						mHandler.sendEmptyMessage(CODE_ELSE);
					}
				});
	}

	@Override
	public void onRefresh() {
		requestData(poiId);
	}

	@Override
	public void onLoadMore() {

	}
}
